package com.test.schoolhelper.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.test.schoolhelper.R;
import com.test.schoolhelper.entity.Weather;

import java.util.ArrayList;
import java.util.List;

// TODO: 2024/5/31 天气适配器 
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder> {

    private Context context;
    private List<Weather.ResultDTO.FutureDTO> list = new ArrayList<>();

    public WeatherAdapter(Context context, List<Weather.ResultDTO.FutureDTO> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Weather.ResultDTO.FutureDTO> list) {
        this.list = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_weather, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        Weather.ResultDTO.FutureDTO info = list.get(position);

        if (info.getWeather().contains("雨")){
            holder.ivIcon.setImageResource(R.drawable.ic_yu);
        }else if (info.getWeather().contains("多云")){
            holder.ivIcon.setImageResource(R.drawable.ic_duoyun);
        }else if (info.getWeather().contains("晴")){
            holder.ivIcon.setImageResource(R.drawable.ic_qing);
        }else if (info.getWeather().contains("阴")){
            holder.ivIcon.setImageResource(R.drawable.ic_yin);
        }

        holder.tvDate.setText(info.getDate());
        holder.tvInfo.setText(info.getWeather()+" "+info.getTemperature()+" "+info.getDirect());

        holder.rlMain.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlMain;
        private ImageView ivIcon;
        private TextView tvDate;
        private TextView tvInfo;

        public ViewHolder(View itemView) {
            super(itemView);
            rlMain = itemView.findViewById(R.id.rl_main);
            ivIcon = itemView.findViewById(R.id.iv_pic);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvInfo = itemView.findViewById(R.id.tv_info);
        }
    }

    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {
        void onClick(int pos);
    }

}
